﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Microsoft.Practices.ObjectBuilder;
using TrainingWorkshop.MVPWebsite.Role.Views;
using TrainingWorkshop.MVPWebsite.Role.Services;
using DataContracts;

namespace TrainingWorkshop.MVPWebsite.Role.Views
{
    public partial class SearchRole : Microsoft.Practices.CompositeWeb.Web.UI.Page, ISearchRoleView
    {
        private SearchRolePresenter _presenter;
        private List<RoleViewDTO> _roleViewList;
        private string _linkButtonId;
        private int _pageIndex;
        private int _pageCount;
        private string _status;
        private string _authorities;
        private string _tempAuthorities;

        #region

        public string RoleName
        {
            get { return RoleNameTextBox.Text.Trim(); }
        }
        public string Status
        {
            get
            {
                if (StatusDropDownList.SelectedItem != null)
                    return StatusDropDownList.SelectedItem.Text.Trim();
                else
                    return "Please Select";
            }
            set { _status = value; }
        }

        public string Authorities
        {
            get
            {

                List<string> _authoritiesList = new List<string>();

                for (int i = 0; i < AuthorityCheckBoxList.Items.Count; i++)
                {
                    if (AuthorityCheckBoxList.Items[i].Selected)
                        _authoritiesList.Add(AuthorityCheckBoxList.Items[i].Text);
                }

                if (_authoritiesList.Count > 0)
                {
                    for (int j = 0; j < _authoritiesList.Count; j++)
                    {
                        _tempAuthorities += _authoritiesList[j];
                        _tempAuthorities += ",";
                    }
                    _tempAuthorities.TrimEnd(',');
                }

                else
                {
                    _tempAuthorities = "";
                }

                _authorities = _tempAuthorities;
                return _authorities;
            }

            set
            {
                _authorities = value;
            }
        }
        public string SortExpression
        {
            get;
            set;

        }
        public string SortDirection
        {
            get;
            set;

        }
        public double PageSize
        {
            get { return Convert.ToDouble(ConfigurationManager.AppSettings["PageSize"]); }
            set { this.PageSize = value; }
        }
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }
        public int PageCount
        {
            get { return _pageCount; }
            set { _pageCount = value; }

        }

        public string ErrorMessage
        {
            get;
            set;
        }

        public string LinkButtonId
        {
            get { return _linkButtonId; }
            set { _linkButtonId = value; }
        }

        public LinkButton linkbutonFirst
        {
            get { return this.lbtnFirst; }
            set { this.lbtnFirst = value; }
        }

        public LinkButton linkbutonPrev
        {
            get { return this.lbtnPrev; }
            set { this.lbtnPrev = value; }
        }

        public LinkButton linkbutonNext
        {
            get { return this.lbtnNext; }
            set { this.lbtnNext = value; }
        }

        public LinkButton linkbutonLast
        {
            get { return this.lbtnLast; }
            set { this.lbtnLast = value; }
        }

        public System.Web.UI.WebControls.Label lableCurrentInfo
        {
            get { return this.lblCurrentInfo; }
            set { this.lblCurrentInfo = value; }
        }

        public System.Web.UI.WebControls.Label lableSlash
        {
            get { return this.lblSlash; }
            set { this.lblSlash = value; }
        }

        public System.Web.UI.WebControls.Label lableCount
        {
            get { return this.lblCount; }
            set { this.lblCount = value; }
        }

        public GridView BindGridView
        {
            get { return this.RoleSearchGridView; }
        }

        public DropDownList dropDownList
        {
            get { return this.StatusDropDownList; }
        }

        public List<StatusDTO> StatusList
        {
            set
            {
                this.StatusDataSource.DataSource = value;
                this.StatusDataSource.DataBind();
            }
        }

        public List<AuthorityDTO> AuthorityList
        {
            set
            {
                this.AuthorityDataSource.DataSource = value;
                this.AuthorityDataSource.DataBind();
            }
        }

        public List<RoleViewDTO> RoleViewList
        {
            get { return _roleViewList; }
            set
            {
                _roleViewList = value;
                this.RoleSearchGridView.DataSource = value;
                this.RoleSearchGridView.DataBind();
                //this.RoleViewDataSource.DataSource = value;
                //this.RoleViewDataSource.DataBind();
            }

        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.RoleSearchGridView.Attributes.Add("SortExpression", "LevelName");
                this.RoleSearchGridView.Attributes.Add("SortDirection", "ASC");
                Session["pageindex"] = 1;
                Session["pagecount"] = 1;
                Session["sortexpressin"] = "LevelName";
                Session["sortdirection"] = "ASC";
                PageIndex = 1;

                this._presenter.OnViewInitialized();

            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public SearchRolePresenter Presenter
        {
            set
            {
                this._presenter = value;
                this._presenter.View = this;
            }
        }

        /// <summary>
        /// Get the Result and display in GridView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GenerateReportButton_Click(object sender, EventArgs e)
        {
            Session["sortexpressin"] = "LevelName";
            Session["sortdirection"] = "ASC";
            SortExpression = "LevelName";
            SortDirection = "ASC";
            PageIndex = 1;

            _presenter.GridBind();
        }

        /// <summary>
        /// Navigate Page Index
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PageIndex_Changed(object sender, EventArgs e)
        {
            LinkButton NavigateButton = (LinkButton)sender;
            LinkButtonId = NavigateButton.ID;
            PageIndex = Convert.ToInt32(Session["pageindex"]);
            PageCount = Convert.ToInt32(Session["pagecount"]);
            _presenter.PageIndexButton_Click();
        }

        /// <summary>
        /// Implement Sorting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RoleSearchGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            //get the sorting data column from event parameters 
            SortExpression = e.SortExpression.ToString();

            //SortDirection = this.RoleSearchGridView.Attributes["SortDirection"].ToString();
            SortDirection = Session["sortdirection"].ToString();

            // compare sorting direction with 'asc',and modified sort direction parameters in GridView
            _presenter.JudgeSortParameter();

            // reset sort datacoloum and sort direction in GridView
            this.RoleSearchGridView.Attributes.Remove("SortExpression");
            this.RoleSearchGridView.Attributes.Remove("SortDirection");
            this.RoleSearchGridView.Attributes.Add("SortExpression", SortExpression);
            this.RoleSearchGridView.Attributes.Add("SortDirection", SortDirection);

            ShowArrow();

            //display the first page
            PageIndex = 1;
            Session["sortexpressin"] = SortExpression;
            Session["sortdirection"] = SortDirection;

            _presenter.GridBind();
        }

        /// <summary>
        /// Binding GridView DataSource
        /// </summary>
        public void GridBind()
        {
            PageCount = 0;
            string a = SortDirection;
           

            SortExpression = Session["sortexpressin"].ToString();
            SortDirection = Session["sortdirection"].ToString();

            _presenter.SearchRole();

            if (_roleViewList.Count == 0)
            {
                _presenter.ShowMessage();
                //EmptyMessage.Visible = true;
                //EmptyMessage.ForeColor = System.Drawing.Color.Red;
                //EmptyMessage.Text = ErrorMessage;


                System.Windows.Forms.MessageBox.Show(ErrorMessage, "Error");

                //Page.RegisterStartupScript("", "<script language='javascript'>window.alert('" + ErrorMessage + "');</script>");
            }
            else
            {
                EmptyMessage.Visible = false;
            }

            Session["pagecount"] = PageCount;
            Session["pageindex"] = PageIndex;
            
            _presenter.CallPageFooterMethod();

        }


        /// <summary>
        /// add arrow to gridview column
        /// </summary>
        public void ShowArrow()
        {
            string[] gridViewHeaderName = { "RoleId", "RoleName", "AuthorityName", "LevelName" };

            for (int i = 0; i < 4; i++)
            {
                if (SortExpression == gridViewHeaderName[i])
                {
                    if (SortDirection == "ASC")

                        BindGridView.Columns[i].HeaderText = gridViewHeaderName[i] + "<font face=webdings>5</font>";

                    else
                        BindGridView.Columns[i].HeaderText = gridViewHeaderName[i] + "<font face=webdings>6</font>";
                }
                else
                    BindGridView.Columns[i].HeaderText = gridViewHeaderName[i];
            }
        }

        /// <summary>
        /// Show search result page information
        /// </summary>
        public void ShowPageInfo()
        {
            lableCount.Text = PageCount.ToString();
            lableCurrentInfo.Text = PageIndex.ToString();
        }

    }
}

